package org.msfj.generator;

import org.apache.ibatis.io.Resources;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.*;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {

    private static Configuration config;
    private static Context context;
    private static JavaModelGeneratorConfiguration modelGeneratorConfiguration;
    private static SqlMapGeneratorConfiguration mapGeneratorConfiguration;

    private static List<String> warnings = new ArrayList<String>();


    public static void main(String[] args) throws Exception {
        init();
        generateMapper();
        generateVo();
        generateServiceImpl();
        generateController();
        generateHtml();
    }

    private static void init() throws Exception {
        URL resourceURL = Resources.getResourceURL("generatorConfig.xml");
        String file = resourceURL.getFile();
        file = java.net.URLDecoder.decode(file, "utf-8");
        File configFile = new File(file);
        ConfigurationParser cp = new ConfigurationParser(warnings);
        config = cp.parseConfiguration(configFile);
        context = config.getContext("Mysql");
        modelGeneratorConfiguration = context.getJavaModelGeneratorConfiguration();
        mapGeneratorConfiguration = context.getSqlMapGeneratorConfiguration();
        //配置要生成的类文件路径
        String moduleName = context.getProperty("moduleName");
        String basePackage = context.getProperty("basePackage");
        String modelPackage = modelGeneratorConfiguration.getTargetPackage();
        String mapperPackage = mapGeneratorConfiguration.getTargetPackage();
        String voPackage = context.getProperty("voPackage");
        String voPath = context.getProperty("voPath");
        String servicePackage = context.getProperty("servicePackage");
        String servicePath = context.getProperty("servicePath");
        String serviceImplPackage = context.getProperty("serviceImplPackage");
        String serviceImplPath = context.getProperty("serviceImplPath");
        String controllerPackage = context.getProperty("controllerPackage");
        String controllerPath = context.getProperty("controllerPath");
        String apiPath = context.getProperty("apiPath");
        String vuePath = context.getProperty("vuePath");
        CodeGenerateFactory.setModuleName(moduleName);
        CodeGenerateFactory.setBasePackage(basePackage);
        CodeGenerateFactory.setModelPackage(modelPackage);
        CodeGenerateFactory.setMapperPackage(mapperPackage);
        CodeGenerateFactory.setVoPackage(voPackage);
        CodeGenerateFactory.setVoPath(voPath);
        CodeGenerateFactory.setservicePackage(servicePackage);
        CodeGenerateFactory.setservicePath(servicePath);
        CodeGenerateFactory.setserviceImplPackage(serviceImplPackage);
        CodeGenerateFactory.setserviceImplPath(serviceImplPath);
        CodeGenerateFactory.setControllerPackage(controllerPackage);
        CodeGenerateFactory.setControllerPath(controllerPath);
        CodeGenerateFactory.setApiPath(apiPath);
        CodeGenerateFactory.setVuePath(vuePath);
    }

    private static void generateMapper() throws Exception {
        boolean overwrite = true;
        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        myBatisGenerator.generate(null);
    }

    /**
     * 生成Vo相关类
     */
    private static void generateVo() {
        //获得所有表的配置信息
        List<TableConfiguration> tableConfigurations = context.getTableConfigurations();
        Iterator<TableConfiguration> iterator = tableConfigurations.iterator();
        while(iterator.hasNext()){
            TableConfiguration table = iterator.next();
            String isTree = table.getProperties().getProperty("isTree");
            CodeGenerateFactory.setIsTree(Boolean.valueOf(isTree));
            CodeGenerateFactory.codeGenerateVo(table.getTableName(),table.getProperty("alias"));
        }
    }

    /**
     * 生成service、impl相关类
     */
    private static void generateServiceImpl() {
        //获得所有表的配置信息
        List<TableConfiguration> tableConfigurations = context.getTableConfigurations();
        Iterator<TableConfiguration> iterator = tableConfigurations.iterator();
        while(iterator.hasNext()){
            TableConfiguration table = iterator.next();
            String isTree = table.getProperties().getProperty("isTree");
            CodeGenerateFactory.setIsTree(Boolean.valueOf(isTree));
            CodeGenerateFactory.codeGenerateServiceImpl(table.getTableName(),table.getProperty("alias"));
        }
    }

    /**
     * 生成service、impl、controller相关类
     */
    private static void generateController() {
        //获得所有表的配置信息
        List<TableConfiguration> tableConfigurations = context.getTableConfigurations();
        Iterator<TableConfiguration> iterator = tableConfigurations.iterator();
        while(iterator.hasNext()){
            TableConfiguration table = iterator.next();
            String isTree = table.getProperties().getProperty("isTree");
            CodeGenerateFactory.setIsTree(Boolean.valueOf(isTree));
            CodeGenerateFactory.codeGenerateController(table.getTableName(),table.getProperty("alias"));
        }
    }

    /**
     * 生成service、impl、controller相关类
     */
    private static void generateHtml() {
        JDBCConnectionConfiguration jdbcConnectionConfiguration = context.getJdbcConnectionConfiguration();
        TableUtil tableUtil = new TableUtil();
        tableUtil.setDataBaseInfo(jdbcConnectionConfiguration.getDriverClass(),jdbcConnectionConfiguration.getConnectionURL(),jdbcConnectionConfiguration.getUserId(),jdbcConnectionConfiguration.getPassword());
        //获得所有表的配置信息
        List<TableConfiguration> tableConfigurations = context.getTableConfigurations();
        Iterator<TableConfiguration> iterator = tableConfigurations.iterator();
        while(iterator.hasNext()){
            TableConfiguration table = iterator.next();
            Map<String, Column> columns = tableUtil.getColumns(table.getTableName());
            String isTree = table.getProperties().getProperty("isTree");
            CodeGenerateFactory.setIsTree(Boolean.valueOf(isTree));
            CodeGenerateFactory.setColumns(columns);
            CodeGenerateFactory.codeGenerateHtml(table.getTableName(),table.getProperty("alias"));
        }
    }

}
